package cn.bestick.weightrecord.dao;

import cn.bestick.weightrecord.model.User;
import cn.bestick.weightrecord.util.StandardPasswordEncoderForSha1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: Bestick
 * Date: 2016/7/8
 * Time: 18:47
 * Url: http://www.bestick.cn
 * Copyright © 2015-2016 Bestick All rights reserved
 * -----------------------------------------------------------
 * 会当凌绝顶，一览众山小。
 */
@Component
public class UserDao extends BaseDao {

    @Value("${sql_ck_select_login_user}")
    private String selectLoginUser;

    @Value("${sql_ck_insert_signup_user}")
    private String insertSignUpUser;

    @Value("${sql_ck_select_user_by_id}")
    private String selectUserById;

    /**
     * 用户登录Dao
     *
     * @param user 入参
     * @return User
     * @throws Exception
     */
    public User login(User user) throws Exception {
        User resultUser = null;
        con = getCon();
        PreparedStatement pstmt = con.prepareStatement(selectLoginUser);
        pstmt.setString(1, user.getUserName());
        pstmt.setString(2, StandardPasswordEncoderForSha1.encode(user.getPassword() + "{" + user.getUserName() + "}"));
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            resultUser = new User();
            resultUser.setUserId(rs.getInt("userId"));
            resultUser.setUserName(rs.getString("userName"));
            resultUser.setPassword(rs.getString("password"));
            resultUser.setSex(rs.getString("sex"));
            resultUser.setExpectedWeight(rs.getDouble("expectedWeight"));
            resultUser.setSignupTime(rs.getTimestamp("signupTime"));
            resultUser.setSignature(rs.getString("signature"));
            resultUser.setEmail(rs.getString("email"));
        }
        close(con);
        return resultUser;
    }

    /**
     * 用户注册
     *
     * @param user 入参
     * @throws Exception
     */
    public void addUser(User user) throws Exception {
        con = getCon();
        PreparedStatement pstmt = con.prepareStatement(insertSignUpUser);
        pstmt.setString(1, user.getUserName());
        pstmt.setString(2, user.getSex());
        pstmt.setString(3, user.getPassword());
        pstmt.setDouble(4, user.getExpectedWeight());
        pstmt.setString(5, user.getSignature());
        pstmt.setString(6, user.getEmail());
        pstmt.setString(7, user.getIp());
        pstmt.setInt(8, user.getState());
        pstmt.executeUpdate();
        close(con);
    }

    /**
     * 通过userId获取user
     *
     * @param userId 用户id
     * @return User
     * @throws Exception
     */
    public User getUserById(Integer userId) throws Exception {
        User resultUser = null;
        con = getCon();
        PreparedStatement pstmt = con.prepareStatement(selectUserById);
        pstmt.setInt(1, userId);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            resultUser = new User();
            resultUser.setUserId(rs.getInt("userId"));
            resultUser.setUserName(rs.getString("userName"));
            resultUser.setPassword(rs.getString("password"));
            resultUser.setExpectedWeight(rs.getDouble("expectedWeight"));
            resultUser.setSex(rs.getString("sex"));
            resultUser.setSignupTime(rs.getTimestamp("signupTime"));
            resultUser.setSignature(rs.getString("signature"));
            resultUser.setEmail(rs.getString("email"));
            resultUser.setIp(rs.getString("ip"));
            resultUser.setState(rs.getInt("state"));
        }
        close(con);
        return resultUser;
    }
}
